//加载更多随访计划
var getMoreList = function () {
    $('.check-more').click(function () {
        var current = $(this);
        if(current.attr('data-loading') == 'true') return false;
        current.attr('data-loading','true').html('<i class="material-icons loading">&#xE5D5;</i>');
        initTemplateList(++window.pageIndex);
    })
};

//新增随访计划相关
var addFollowPlan = function () {
    //打开弹窗
    $('.operate-box .main-btn').click(function () {
        new followupPlan().getTemplateList(function (res,status) {
            res = JSON.parse(res);
            if(res.code == '000'){
                var resData = JSON.parse(res.data);
                var len = resData.length;
                var html = '';
                for(var i = 0;i < len; i++){
                    html += '<option value="'+ resData[i].id +'">'+ resData[i].name +'</option>';
                }
                $('#select111').html(html);
            }
        });
        $('#addFollowModal').modal();
    });
    //手机号码匹配姓名
    $('#inputPhone').on('change keyup',function () {
        var val = $(this).val().trim();
        if(val.length == 11 && typeof +val == 'number'){
            new followupPlan().getPatientNameByPhone(val,function (res,status) {
                res = JSON.parse(res);
                var patientName = JSON.parse(res.data).name;
                $('#inputName').val(patientName);
            });
        }
    });
    //新建随访计划
    $('#enterCreateBtn').click(function () {
        var planModal = {
            "template": $('#select111').val(),
            "phone": $('#inputPhone').val(),
            "patientname": $('#inputName').val(),
            "starttime": $('#selectTime').val()
        };
        if(!planModal.template || !planModal.phone || !planModal.patientname || !planModal.starttime){
            $$.modal.alert('内容不为能空','关闭');
            return false;
        }
        new followupPlan().createNewPlan(planModal,function (res, status) {
            res = JSON.parse(res);
            if(res.code == '000'){
                $('#addFollowModal').modal('hide');
                location.reload();
            }
        });
    });
};
//选择开始日期
var timepicker=function () {
    $('#selectTime').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayBtn: true,
        minView:2,
        pickerPosition:"top-right"
    });
};
//初始化随访列表
var initTemplateList = function (pageIndex) {
    var pageSize = 10;
    var urlParamObj = $$.util.urlParamToObj();
    var filterParam = (urlParamObj.template && {template : urlParamObj.template}) || (urlParamObj.hospital && {hospital : urlParamObj.hospital}) || (urlParamObj.doctor && {creator : urlParamObj.doctor}) || {};
    $('.outer').addClass('active');
    new followupPlan().goingFulist(JSON.parse(localStorage.userInfo).userid,pageIndex,pageSize,+$('body').attr('data-sort'),filterParam,function (res, status) {
        res = JSON.parse(res);
        if(res.code == '000'){
            var resData = JSON.parse(JSON.parse(res.data).data);
            var len = resData.length;
            var html ='';
            for(var i=0;i<len;i++){
                var avatar = 'http://flup.leley.com/cholecystectomy/assets/avatars/avatar4.png';
                if(resData[i].headphoto) avatar = urlParam.basePictureUrl + resData[i].headphoto;
                var lastStepName = '';
                if(resData[i].recentlytime){
                    var stepLen = resData[i].steps.length;
                    for(var j = 0;j<stepLen;j++){
                        if(resData[i].recentlytime == resData[i].steps[j].enddatetime){
                            lastStepName = resData[i].steps[j].title;
                            break;
                        }
                    }
                }
                html+=[
                    '<li class="fu-item">',
                    '<a href="./node.html?planid='+ resData[i].id +'&status='+ resData[i].status +'&num='+ resData[i].currentPage +'&total='+ resData[i].stepcount +'" class="fu-item-link">',
                    '<div class="info-box">',
                    '<div class="face">',
                    '<img src="'+ avatar +'">',
                    '</div>',
                    '<div class="name-box">',
                    '<p class="name">'+ resData[i].patientname +'</p>',
                    '<p class="type">'+ resData[i].name +'</p>',
                    '</div>',
                    '</div>',
                    '<div class="time-box">',
                    '<p class="date">'+ ((resData[i].recentlytime && $$.util.timeStampToDay(resData[i].recentlytime)) || "未设置时间") +'</p>',
                    '<p class="after-treat">'+ lastStepName +'</p>',
                    '</div>',
                    '</a>',
                    '</li>'
                ].join('');
            }
            $('#moreListBox').append(html);
            $(".check-more").attr("data-loading", "false").text("查看更多随访计划");
            if(len < pageSize) $('.check-more').addClass('disabled').attr('data-loading','true').text('没有更多了~');
            if(pageIndex == 1) return false;
            $("html,body").animate({
                scrollTop: $("body").scrollTop() + 10 * $(".fu-item").height()
            }, 500);
        }
    });
};
$(function () {
    getMoreList();
    addFollowPlan();
    timepicker();
    window.pageIndex = 1;
    initTemplateList(window.pageIndex);
});